﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Yipusoft.SecurityEvaluate;
using Yipusoft.Account.BLL;
using Yipusoft.Account.Model;
using System.Web.UI.DataVisualization.Charting;
using System.Data;
using Yipusoft.VehicleReport.BLL;
public partial class Police_SecurityEvaluate_SquadronAnlyseVehicleDay : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindDateList();
            if (Request["Departmentid"] != null)
            {

                DptMonth.SelectedValue = Request["m"];
                DptYear.SelectedValue = Request["y"];
            }
            BindDate();
        }
    }

    private void BindDate()
    {

        string department = Yipusoft.Account.BLL.User.GetUser(User.Identity.Name).DepartmentID;
        if (Request["Departmentid"] != null)
        {
            department = Request["Departmentid"];

        }
        Caption.Text = "（" + Yipusoft.Account.BLL.Department.GetDepartment(department).ShortName + "）车辆装备检查统计分析";

        List<ChartItem> list = new List<ChartItem>();
        int year = Convert.ToInt32(DptYear.SelectedValue);
        int month = Convert.ToInt32(DptMonth.SelectedValue);
        int monthDayCount = Account.GetMonthDayCount(year, month);
        //总数
        int diaryTotal = monthDayCount;

        List<DepartmentInfo> departments = Department.GetList("departmentid like '" + department.Substring(0, 4) + "3%' and state=0 ");
        foreach (DepartmentInfo info in departments)
        {
            //中队个数
            int totalCount = monthDayCount;
            string where = " departmentId = '" + info.DepartmentID + "' and year(Daydate)=" + year + " and month(daydate)=" + month + " ";
            int RealCount = VehicleDayItem.GetVichileDayCount(where);
            double percent = (double)(totalCount - RealCount) / totalCount;
            list.Add(new ChartItem()
            {
                DepartmentId = info.DepartmentID,
                DepartmentName = info.ShortName,
                Percent = percent
            });

        }
        list = list.OrderByDescending(o => o.Percent).ToList();

        UserChart.Series["车辆装备检查"].Points.DataBindXY(list, "DepartmentName", list, "Percent");
        UserChart.ChartAreas["ChartArea1"].AxisX.IsLabelAutoFit = false;
        UserChart.Series["车辆装备检查"].LabelFormat = "0.0" + "%";
        for (int i = 0; i < list.Count; i++)
        {
            UserChart.Series["车辆装备检查"].Points[i].Url = "List.aspx?Departmentid=" + Convert.ToString(list[i].DepartmentId) + "&y=" + DptYear.SelectedValue + "&m=" + DptMonth.SelectedValue;
        }
    }


    private void BindDateList()
    {
        for (int i = 2008; i <= 2050; i++)
        {
            ListItem li = new ListItem();
            li.Text = i.ToString() + "年";
            li.Value = i.ToString();
            DptYear.Items.Add(li);
        }
        for (int i = 1; i <= 12; i++)
        {
            this.DptMonth.Items.Add(new ListItem(i.ToString() + "月", i.ToString()));
        }
        if (Request.QueryString["year"] == null)
        {
            DptMonth.SelectedValue = DateTime.Now.Month.ToString();
            DptYear.SelectedValue = DateTime.Now.Year.ToString();
        }
        else
        {
            DptMonth.SelectedValue = Request["month"];
            DptYear.SelectedValue = Request["year"];
        }
    }

    protected void DptYear_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindDate();
    }
    protected void DptMonth_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindDate();
    }
    protected void ddlSort_SelectedIndexChanged(object sender, EventArgs e)
    {
        BindDate();
    }

    [Serializable]
    public class ChartItem
    {
        public string DepartmentId
        {
            set;
            get;
        }

        public string DepartmentName
        {
            set;
            get;
        }
        public double Percent
        {
            set;
            get;
        }
    }
}
